package com.dyw.spzx.manager.service.lmpl;

import com.alibaba.excel.EasyExcel;
import com.dyw.spzx.common.excepiton.GuiguException;
import com.dyw.spzx.manager.listener.ExcelListener;
import com.dyw.spzx.manager.mapper.CategoryMapper;
import com.dyw.spzx.manager.service.CategoryService;
import com.dyw.spzx.model.entity.product.Category;
import com.dyw.spzx.model.vo.common.ResultCodeEnum;
import com.dyw.spzx.model.vo.product.CategoryExcelVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    // 根据parentId获取下级节点
    @Override
    public List<Category> findByParentId(Long parentId) {

        List<Category> categoryList = categoryMapper.findByParentId(parentId);
        if (!CollectionUtils.isEmpty(categoryList)) {
            categoryList.forEach(category -> {
                int count = categoryMapper.countByParentId(category.getId());
                if (count > 0) {
                    category.setHasChildren(true);
                } else {
                    category.setHasChildren(false);
                }
            });
        }

        return categoryList;
    }

    // 导出数据
    @Override
    public void exportData(HttpServletResponse httpServletResponse) {

        try {
            // 设置响应结果类型
            httpServletResponse.setContentType("application/vnd.ms-exce");
            httpServletResponse.setCharacterEncoding("utf-8");

            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("分类数据", "UTF-8");
            httpServletResponse.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            // 查询数据库中的数据
            List<Category> categoryList = categoryMapper.selectAll();
            // 将Category转换成CategoryExcelVo类型
            List<CategoryExcelVo> categoryExcelVoList = new ArrayList<>();
            for (Category category : categoryList) {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(category, categoryExcelVo, CategoryExcelVo.class);
                categoryExcelVoList.add(categoryExcelVo);
            }

            // 写出数据库数据到浏览端
            EasyExcel.write(httpServletResponse.getOutputStream(), CategoryExcelVo.class)
                    .sheet("分类数据").doWrite(categoryExcelVoList);

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuiguException(ResultCodeEnum.ERROR_DATA);
        }


    }


    // 导入数据
    @Override
    public void importData(MultipartFile multipartFile) {


        try {
            // 创建监听器对象
            ExcelListener<CategoryExcelVo> excelListener = new ExcelListener<>(categoryMapper);
            // 调用read方法读取Excel数据
            EasyExcel.read(multipartFile.getInputStream(),
                    CategoryExcelVo.class,
                    excelListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuiguException(ResultCodeEnum.ERROR_DATA);
        }


    }
}
